package test.vfx.depre;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.vfx.AbstractGameEffect;
import test.actions.depre.BlockAction;
import test.monitors.SeaMonitor;
import test.utils.depre.sakis.Saki;

public class SakiMoveEffect extends AbstractGameEffect {

    public static final float Y_POSTION = 120.0F;
    public static final float MOVE_RIGHT_SPEED = 400.0F;
    public static final float MOVE_DOWN_SPEED = 400.0F;

    public Saki saki;
    public boolean isRight;
    public AbstractGameAction blockAction;
    public int currentIndex;

    private enum MoveState {
        MOVE_RIGHT,
        MOVE_LEFT,
        MOVE_DOWN
    }

    private MoveState currentState;

    public SakiMoveEffect(Saki saki,AbstractCard card,AbstractGameAction blockAction) {
        this.saki = saki;
        isRight=true;
        this.blockAction=blockAction;
        AbstractDungeon.actionManager.addToBottom(this.blockAction);
        this.currentIndex= SeaMonitor.getSeaByCard(card).index;
        saki.x = SeaMonitor.BLANK_SEA_POSITIONS_X.get(currentIndex)- saki.SAKI_WIDTH /2-AbstractCard.IMG_WIDTH_S/2;
        saki.y = Y_POSTION*Settings.scale;
        this.saki.canMoveDistance--;
        currentState = MoveState.MOVE_RIGHT;
    }

    public void update() {
        if (isDone) return;
        saki.update();

        switch (currentState) {
            case MOVE_RIGHT:
                updateRightMovement();
                break;
            case MOVE_LEFT:
                updateLeftMovement();
                break;
            case MOVE_DOWN:
                updateDownMovement();
                break;
        }
    }

    private void updateRightMovement() {
        float targetX = getTargetX(currentIndex + 1);
        if (saki.x < targetX) {
            saki.x += MOVE_RIGHT_SPEED * Gdx.graphics.getDeltaTime();
        } else {
            handleStateTransition(true);
        }
    }

    private void updateLeftMovement() {
        float targetX = getTargetX(currentIndex - 1);
        if (saki.x > targetX) {
            saki.x -= MOVE_RIGHT_SPEED * Gdx.graphics.getDeltaTime();
        } else {
            handleStateTransition(false);
        }
    }

    private void updateDownMovement() {
        saki.y -= MOVE_DOWN_SPEED * Gdx.graphics.getDeltaTime();
        if(saki.y < -100.0f) {
            AbstractCard card= getCurrentCard();
            if(card!=null){
                saki.triggerOnDown(card);
            }
            blockAction.isDone = true;
            isDone = true;
        }
    }

    private float getTargetX(int index) {
        if (index < SeaMonitor.MAX_HAND_SIZE) {
            return SeaMonitor.BLANK_SEA_POSITIONS_X.get(index) - saki.SAKI_WIDTH /2;
        }
        return SeaMonitor.INDEX_10 - saki.SAKI_WIDTH /2;
    }

    private void handleStateTransition(boolean movingRight) {
//        int targetIndex;
//        if (movingRight) {
//            targetIndex = currentIndex + 1;
//        } else {
//            targetIndex = currentIndex - 1;
//        }
        if (movingRight) {
            if (currentIndex < SeaMonitor.MAX_HAND_SIZE - 1) {
                currentIndex++;
            }
        } else {
            if (currentIndex > 0) {
                currentIndex--;
            }
        }
        AbstractCard card= getCurrentCard();
        if(saki.canMoveDistance>0){
            blockAction.isDone = true;
            saki.canMoveDistance--;
            //blockAction.isDone = true;
            blockAction = new BlockAction();
            AbstractDungeon.actionManager.addToTop(blockAction);
        }else{
            currentState = MoveState.MOVE_DOWN;
        }
    }

    public AbstractCard getCurrentCard() {
        if(currentIndex < SeaMonitor.MAX_HAND_SIZE&&currentIndex>=0) {
            return SeaMonitor.seaList.get(currentIndex).card;
        }
        return null;
    }

    public void render(SpriteBatch sb) {
        if(isDone){
            return;
        }
        saki.render(sb);
    }

    public void dispose() {

    }
}